﻿'/*
'* This file is part of WinMergeFS.
'*
'* WinMergeFS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU General Public License as published by
'* the Free Software Foundation, either version 2 of the License, or
'* (at your option) any later version.

'* WinMergeFS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU General Public License for more details.
'*
'* You should have received a copy of the GNU General Public License
'* along with WinMergeFS.  If not, see <http://www.gnu.org/licenses/>.
'*/
Public Class Form1
    Dim WithEvents mfs As New WinMergeFS.API.WinMergeFS()
    Dim localprofile As Profile
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim msg As String = ""
        If mfs.MountedDrives.Count <> 0 Then
            For Each mp In mfs.MountedDrives.Values
                msg = msg & vbCrLf & mp.Description
            Next
        Else
            msg = "nothing mounted"
        End If
        MsgBox(msg)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim driveletter As String = InputBox("Unmount drive")
        mfs.UnmountDrive(mfs.MountedDrives(driveletter(0)))
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GUItoProfile()
        mfs.MountDrive(localprofile)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mfs.UnmountAll()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        GUItoProfile()
        mfs.MountDriveExclusive(localprofile)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        mfs.UnmountAll()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        mfs.UnmountAll()
        End
    End Sub

    Private Sub mfs_Logger(ByVal message As String, ByVal level As LogLevel) Handles mfs.Logger
        If Me.InvokeRequired Then
            Dim addmethod As New AddLogEntryDelegate(AddressOf AddLogEntry)

            Me.Invoke(addmethod, message, level)
        Else
            AddLogEntry(message, level)
        End If
    End Sub

    Private Sub AddLogEntry(ByVal message As String, ByVal level As LogLevel)
        lsbLog.Items.Add(message)
        lsbLog.SelectedIndex = lsbLog.Items.Count - 1
    End Sub

    Private Delegate Sub AddLogEntryDelegate(ByVal message As String, ByVal level As LogLevel)

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveProfile.Click
        GUItoProfile()
        ProfileManager.SaveProfileToFile(localprofile, savefilename.Text)
    End Sub

    Private Sub GUItoProfile()
        Dim roots As New List(Of RootEntry)
        roots.Add(New RootEntry(txtRoot1.Text, True))
        roots.Add(New RootEntry(txtRoot2.Text, True))

        Dim plugins As New Dictionary(Of String, ProfilePluginEntry)
        plugins.Add(TextBox2.Text, New ProfilePluginEntry(TextBox2.Text, Convert.ToInt32(TextBox1.Text), Double.Parse(TextBox3.Text, System.Globalization.CultureInfo.InvariantCulture.NumberFormat), Double.Parse(TextBox4.Text, System.Globalization.CultureInfo.InvariantCulture.NumberFormat)))
        localprofile = ProfileManager.NewProfile(txtProfileID.Text, txtVolumeID.Text, txtDriveLetter.Text(0), roots, plugins)
    End Sub

    Private Sub ProfileToGUI()
        txtDriveLetter.Text = localprofile.DriveLetter
        txtProfileID.Text = localprofile.Description
        txtVolumeID.Text = localprofile.VolumeID
        txtRoot1.Text = localprofile.Roots.Item(0).RootPath
        txtRoot2.Text = localprofile.Roots.Item(1).RootPath
        TextBox1.Text = localprofile.Plugins.Item("diskspace").Position
        TextBox2.Text = localprofile.Plugins.Item("diskspace").PluginID
        TextBox3.Text = localprofile.Plugins.Item("diskspace").Multiply.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat)
        TextBox4.Text = localprofile.Plugins.Item("diskspace").Parameter.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat)

    End Sub

    Private Sub LoadProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadProfile.Click
        localprofile = ProfileManager.LoadProfileFromFile(savefilename.Text)
        ProfileToGUI()
    End Sub
End Class
